package com.tinem.platform.web.netty.websocket.message;

import com.tinem.platform.module.pojo.enums.message.Message;
import com.tinem.platform.module.starter.jms.consumer.ConsumerService;
import com.tinem.platform.module.starter.jms.consumer.SubscribeListener;
import com.tinem.platform.module.starter.jms.enums.TopicEnum;
import com.tinem.platform.web.netty.websocket.session.Session;
import com.tinem.platform.web.netty.websocket.util.MessageUtil;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className EchoCommand
 * @description TODO
 * @site
 * @company
 * @create 2021/11/30 19:30:48
 */
@Service("command_session")
public class SessionCommand implements Command{

    @Value("${spring.application.name}")
    protected String name;
    @Value("${spring.profiles.active}")
    protected String active;

    @Resource
    ConsumerService consumerService;
    @Override
    public void call(ChannelHandlerContext ctx, Message message) {
        Session.addSession(ctx);

        String connectionId = consumerService.subscribeTopic(TopicEnum.getAllSession(active,name), new SubscribeListener<Message>() {
            @Override
            public void call(Message message1) {
                MessageUtil.sendMessage(ctx, message1);
            }
        });
        Session.addConsumerId(ctx,connectionId);

        MessageUtil.sendMessage(ctx, message);
    }
}
